package com.nurese.controller;


import com.nurese.entity.*;
import com.nurese.service.*;
import com.nurese.utils.MapControll;
import com.nurese.utils.MapParameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {

    @Autowired
    AdministratorService administratorService;
    @Autowired
    UserService userService;
    @Autowired
    PatientService patientService;
    @Autowired
    EvaluateService evaluateService;
    @Autowired
    CareworkerService careworkerService;
    @Autowired
    OrderService orderService;


//    @GetMapping("/index")
    @RequestMapping("/index")
    public String index(){
        return "index";
    }

    @RequestMapping("/pwd")
    public String pwd(){
        return "pwd";
    }

    @PostMapping("/pwd")
    @ResponseBody
    public Map<String,Object> pwd(Integer id,String sourcePwd,String newPwd){
        Administrator administrator = administratorService.detail(id);
        if(administrator.getPassword().equals(sourcePwd)){
            Administrator entity = new Administrator();
            entity.setId(id);
            entity.setPassword(newPwd);
            int update = administratorService.update(entity);
            if(update>0){
                return MapControll.getInstance().success().getMap();
            }else{
                return MapControll.getInstance().error().getMap();
            }
        }else{
            return MapControll.getInstance().error("原密码错误").getMap();
        }
    }
//    @GetMapping("/main")
//    public String main(ModelMap modelMap){
//        List<Map<String,Object>> mapList = new ArrayList<>();
//        List<Order> orders= orderService.query(null);
//        List<Careworker> careworkers= careworkerService.query(null);
////        for (Careworker careworker:careworkers){
//            for (Order order:orders){
//            Map<String,Object> map=new HashMap<>();
//            map.put("name",order.getWorkername() );
//            int cnt=0;
////            for (Careworker careworker:careworkers){
////                if (careworker.getWorkername()==order.getWorkername()){
////                    cnt++;
////                }
////            }
//
////            map.put("cnt", cnt);
//            modelMap.put("orderCnt", orders.size());
//            mapList.add(map);
//        }
////        List<User> users= userService.query(null);
////        List<Patient> patients= patientService.query(null);
////        List<Order> orders= orderService.query(null);
////        List<Evaluate> evaluates= evaluateService.query(null);
////        List<Careworker> careworkers= careworkerService.query(null);
////        modelMap.put("userCnt", users.size());
////        modelMap.put("patientCnt", patients.size());
////        modelMap.put("orderCnt", orders.size());
////        modelMap.put("evaluateCnt", evaluates.size());
////        modelMap.put("careworkerCnt", careworkers.size());
//        modelMap.put("mapList",mapList);
//        return "main";
//    }

    /**
     * 首页数字统计
     * @return
     */
    @PostMapping("/sum")
    @ResponseBody
    public Map<String,Object> sum(){
        List<User> users= userService.query(null);
        List<Patient> patients= patientService.query(null);
        List<Order> orders= orderService.query(null);
        List<Evaluate> evaluates= evaluateService.query(null);
        List<Careworker> careworkers= careworkerService.query(null);
        Map<String,Object> map = MapParameter.getInstance()
                .add("userCnt", users.size())
                .add("patientCnt", patients.size())
                .add("orderCnt", orders.size())
                .add("evaluateCnt", evaluates.size())
                .add("careworkerCnt", careworkers.size())
                .getMap();
        return MapControll.getInstance().success().add("data",map).getMap();
    }

}
